#include "hand.h"
#include <algorithm>

hand::hand(vector<card> initialHand_){

	initialHand.resize(5);
	initialHand=initialHand_;
	toCompare=vector<int>(6,0);
}

const int hand::toCompareArr(int n) const{

	return toCompare[n];
}

vector<card> hand::getSortedHand(){

	return initialHand;
}

std::string hand::getHandName(){

	return handName;
}

void hand::handOrganizer(){

	sort(initialHand.begin(),initialHand.end(),cardSortPre);

	vector<card>::iterator itc;
	vector<card>::iterator its;

	for (itc=initialHand.begin();itc!=initialHand.end();++itc){
		itc->countOfCard=1;
	}

	int i=1;
	while(i<5){
		++i;
		for (itc=initialHand.begin();itc!=initialHand.end();++itc){
			its=search_n(initialHand.begin(),initialHand.end(),i,*itc,cardComp);
			if (its!=initialHand.end()){
				itc->countOfCard=i;
			}
		}

	}


	sort(initialHand.begin(),initialHand.end(),cardSort);


	itc=initialHand.begin();
	if (itc->countOfCard==4){
		toCompare[0]=8;
		toCompare[1]=itc->cardRank();
		itc+=4;
		toCompare[2]=itc->cardRank();
		handName="Quads of "+rankString[toCompare[1]]+" with "+rankString[toCompare[2]];

	}

	else if (itc->countOfCard==3){
		toCompare[1]=itc->cardRank();
		if ((*(itc+3)).countOfCard==2){
			toCompare[0]=7;
			itc+=3;
			toCompare[2]=itc->cardRank();
			handName="Full house "+rankString[toCompare[1]]+" full of "+rankString[toCompare[2]]+"s";
		}
		else{
			toCompare[0]=4;
			itc+=3;
			toCompare[2]=itc->cardRank();
			itc+=1;
			toCompare[3]=itc->cardRank();
			handName="Set of "+rankString[toCompare[1]]+" high card of "+rankString[toCompare[2]];

		}
	}

	else if (itc->countOfCard==2){
		toCompare[1]=itc->cardRank();
		if ((*(itc+2)).countOfCard==2){
			toCompare[0]=3;
			itc+=2;
			toCompare[2]=itc->cardRank();
			itc+=2;
			toCompare[3]=itc->cardRank();
			handName="Two pairs of "+rankString[toCompare[1]]+" & "+rankString[toCompare[2]]+" high card of "+rankString[toCompare[3]];

		}
		else{
			toCompare[0]=2;
			itc+=2;
			toCompare[2]=itc->cardRank();
			itc+=1;
			toCompare[3]=itc->cardRank();
			itc+=1;
			toCompare[4]=itc->cardRank();
			handName="One pair of "+rankString[toCompare[1]]+" high card of "+rankString[toCompare[2]];
			
		}

	}

	else{

		toCompare[1]=itc->cardRank();

		its=search_n(initialHand.begin(),initialHand.end(),5,*itc,cardCompSuit);
		if (its!=initialHand.end()){
			if ( itc->cardRank()-(*(itc+4)).cardRank()==4){
				toCompare[0]=9;
				handName=" Straight flush of "+suitString[itc->cardSuit()]+" & "+rankString[toCompare[1]];
			}
			else{

				toCompare[0]=6;
				handName=" Flush of "+suitString[itc->cardSuit()]+" & high of "+rankString[toCompare[1]];
			}

		}
		else{

			if (itc->cardRank()-(*(itc+4)).cardRank()==4){
				toCompare[0]=5;
				handName=" Straight of "+rankString[toCompare[1]];
			}
			else{

				toCompare[0]=1;
				handName=" High card of "+rankString[toCompare[1]];
			}

		}

		itc+=1;
		toCompare[2]=itc->cardRank();
		itc+=1;
		toCompare[3]=itc->cardRank();
		itc+=1;
		toCompare[4]=itc->cardRank();
		itc+=1;
		toCompare[5]=itc->cardRank();
	
	}

}


bool handSort(const hand& left,const hand& right){

	for (int i=0;i<6;++i){
		if (left.toCompare[i]!=right.toCompare[i]){
			return left.toCompare[i]>right.toCompare[i];
		}
	}
	return false;
}